package org.practice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zch
 */
public class RomanNumber {

    public static void main(String[] args) {
        int[] nums = {2, 4, 19, 68, 473, 666, 3999};
        String[] answers = {"II", "IV", "XIX", "LXVIII", "CDLXXIII", "DCLXVI", "MMMCMXCIX"};
        for (int i = 0; i < nums.length; i++) {
            assert answers[i].equals(convert(nums[i]));
        }
    }

    private static String convert(int num) {
        int[] arab = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] roman= { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        if (num < 1 || num > 3999 ) {
            throw new IllegalArgumentException("数字范围超过限制！");
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < arab.length; i++) {
            while (num >= arab[i]) {
                result.append(roman[i]);
                num -= arab[i];
            }
        }
        return result.toString();
    }
}
